package com.songxian.wechatcompose.ui

import android.util.Log
import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.songxian.wechatcompose.R
import com.songxian.wechatcompose.WeViewModel
import com.songxian.wechatcompose.ui.theme.WeTheme


@Composable
fun HomeBottomBar(current: Int, curentCanged: (Int) -> Unit) {
    WeBottomBar {
        HomeBottomItem(
            Modifier
                .weight(1f)
                .clickable { curentCanged(0) },
            if (current == 0) R.drawable.ic_chat_filled else R.drawable.ic_chat_outlined,
            "聊天",
            if (current == 0) WeTheme.colors.iconCurrent else WeTheme.colors.icon
        )
        HomeBottomItem(
            Modifier
                .weight(1f)
                .clickable { curentCanged(1) },
            if (current == 1) R.drawable.ic_contacts_filled else R.drawable.ic_contacts_outlined,
            "通讯录",
            if (current == 1) WeTheme.colors.iconCurrent else WeTheme.colors.icon
        )
        HomeBottomItem(
            Modifier
                .weight(1f)
                .clickable { curentCanged(2) },
            if (current == 2) R.drawable.ic_discover_filled else R.drawable.ic_discover_outlined,
            "发现",
            if (current == 2) WeTheme.colors.iconCurrent else WeTheme.colors.icon
        )
        HomeBottomItem(
            Modifier
                .weight(1f)
                .clickable { curentCanged(3) },
            if (current == 3) R.drawable.ic_me_filled else R.drawable.ic_me_outlined,
            "我",
            if (current == 3) WeTheme.colors.iconCurrent else WeTheme.colors.icon
        )
    }
}

@Composable
fun HomeBottomItem(modifier: Modifier, @DrawableRes iconId: Int, title: String, tint: Color) {
    Column(
        modifier
            .padding(0.dp, 8.dp, 0.dp, 8.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Icon(
            painter = painterResource(id = iconId),
            contentDescription = title,
            Modifier.size(24.dp),
            tint = tint
        )
        Text(text = title, fontSize = 11.sp, color = tint)
    }
}


@Composable
fun Home() {
    val viewModel: WeViewModel = viewModel()
    Box {
        Column {
            Box(
                Modifier
                    .weight(1f)
            ) {
                when (viewModel.homeCurrentPage) {
                    0 -> {
                        ChatList()
                    }
                    1 -> {
                        ContactList()
                    }
                    2 -> {
                        DiscoveryList()
                    }
                    3 -> {
                        MeList()
                    }
                }
            }

            HomeBottomBar(current = viewModel.homeCurrentPage) {
                viewModel.homeCurrentPage = it
            }
        }
        if (viewModel.currentChat != null) {
            ChatPage(viewModel.currentChat!!) {

            }
        }
    }

}

@Preview
@Composable
fun PreviewHomeBottomBar() {
    HomeBottomBar(0) {}
}

@Preview
@Composable
fun PreviewHome() {
    Home()
}